#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.request
import gzip
import json

import urllib
import sys
import re
#import pinyin

citys = ["上海","北京","北京市","朝阳","朝阳区","海淀","元谋","南充",\
         "且末","荔波","佛山","庆阳","保山","二连浩特","丽江","北京","锦州","塔城","大庆","西安",\
"芷江","西宁","喀什","三亚","恩施","三女河","连云港","长春","牡丹江","伊春","山海关","鸡西","昭通","达州","海淀","宜宾","运城","安阳","酒泉","通辽","济宁","重庆",\
"延吉","黑河","无锡","榆林","九江","兴城","乌海","邢台","阿克苏","厦门","晋江","黎平","昆明","深圳","宁波","文山","临沂","伊宁","大理","揭阳","邯郸","南阳","大同",\
"腾冲","银川","呼和浩特","合肥","乌兰浩特","襄樊","柳州","南宁","长海","蓬莱","缨芬河","满洲里","成都","安顺","珠海","库尔勒","通化","兴宁","长沙","依兰","西双版纳",\
"温州","万县","延安","上海","安康","潍坊","广汉","武汉","佳木斯","博尔塔拉","长治","湛江","哈尔滨","思茅","富蕴","鞍山","兴义","鄂尔多斯","天津","常德","杭州","朝阳",\
"拉萨","长白山","烟台","福州","梁平","泸州","芒市","临沧","安庆","保安营","济南","蚌埠","石家庄","南昌","张家界","吉林","吉安","海拉尔","遵义","朝阳区","铜仁","南通",\
"广州","太原","阜阳","齐齐哈尔","大连","景德镇","徐州","贵阳","苏州","东营","常州","丹东","梧州","永州","衢州","黄山","惠州","新源","阿尔山","百色","兰州","荆州",\
"梅州","和田","扬州","海口","林芝","青岛","哈密","库车","洛阳","嘉峪关","沈阳","绵阳","克拉玛依","玉树","西昌","汉中","赣州","威海","迪庆","盐城","武夷山","阿勒泰",\
"乌鲁木齐","庐山","秦皇岛","格尔木","衡阳","桂林","义乌","连城","井冈山","宜昌","昌都","赤峰","九寨沟","锡林浩特","黄岩","敦煌","南京","舟山","郑州","北海","淮安",\
"固原","包头","广元","康定","万县","三亚","三女河","上海","且末","东营","临沂","临沧","丹东","丽江","义乌","乌兰浩特","乌海","乌鲁木齐","九寨沟","九江","二连浩特",\
"井冈山","伊宁","伊春","佛山","佳木斯","依兰","保安营","保山","元谋","克拉玛依","兰州","兴义","兴城","兴宁","包头","北京","北海","南京","南充","南宁","南昌","南通",\
"南阳","博尔塔拉","厦门","合肥","吉安","吉林","呼和浩特","和田","哈密","哈尔滨","喀什","嘉峪关","固原","塔城","大同","大庆","大理","大连","天津","太原","威海","宁波",\
"安庆","安康","安阳","安顺","宜宾","宜昌","富蕴","山海关","常州","常德","广元","广州","广汉","庆阳","庐山","库尔勒","库车","康定","延吉","延安","张家界","徐州","思茅",\
"恩施","惠州","成都","扬州","拉萨","揭阳","敦煌","文山","新源","无锡","昆明","昌都","昭通","晋江","景德镇","朝阳","杭州","林芝","柳州","格尔木","桂林","梁平","梅州",\
"梧州","榆林","武夷山","武汉","永州","汉中","沈阳","泸州","洛阳","济南","济宁","海口","海拉尔","淮安","深圳","温州","湛江","满洲里","潍坊","烟台","牡丹江","玉树","珠海",\
"百色","盐城","石家庄","福州","秦皇岛","绵阳","缨芬河","腾冲","舟山","芒市","芷江","苏州","荆州","荔波","蓬莱","蚌埠","衡阳","衢州","襄樊","西双版纳","西宁","西安","西昌",\
"贵阳","赣州","赤峰","达州","运城","连云港","连城","迪庆","通化","通辽","遵义","邢台","邯郸","郑州","鄂尔多斯","酒泉","重庆","铜仁","银川","锡林浩特","锦州","长春","长沙",\
"长治","长海","长白山","阜阳","阿克苏","阿勒泰","阿尔山","青岛","鞍山","鸡西","黄山","黄岩","黎平","黑河","齐齐哈尔",
"株洲","茶陵"]


def get_weather_data() :
    city_name = input('请输入要查询的城市名称：')
    url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
    url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
    #网址1只需要输入城市名，网址2需要输入城市代码
    #print(url1)
    weather_data = urllib.request.urlopen(url1).read()
    #读取网页数据
    weather_data = gzip.decompress(weather_data).decode('utf-8')
    #解压网页数据
    weather_dict = json.loads(weather_data)
    #将json数据转换为dict数据
    return weather_dict

def show_weather(weather_data):
    weather_dict = weather_data
    #将json数据转换为dict数据
    if weather_dict.get('desc') == 'invilad-citykey':
        print('你输入的城市名有误，或者天气中心未收录你所在城市')
    elif weather_dict.get('desc') =='OK':
        forecast = weather_dict.get('data').get('forecast')
        print('城市：',weather_dict.get('data').get('city'))
        print('温度：',weather_dict.get('data').get('wendu')+'℃ ')
        print('感冒：',weather_dict.get('data').get('ganmao'))
        print('风向：',forecast[0].get('fengxiang'))
        print('风级：',forecast[0].get('fengli'))
        print('高温：',forecast[0].get('high'))
        print('低温：',forecast[0].get('low'))
        print('天气：',forecast[0].get('type'))
        print('日期：',forecast[0].get('date'))
        print('*******************************')
        four_day_forecast =input('是否要显示未来四天天气，是/否：')
        if four_day_forecast == '是' or 'Y' or 'y':
            for i in range(1,5):
                print('日期：',forecast[i].get('date'))
                print('风向：',forecast[i].get('fengxiang'))
                print('风级：',forecast[i].get('fengli'))
                print('高温：',forecast[i].get('high'))
                print('低温：',forecast[i].get('low'))
                print('天气：',forecast[i].get('type'))
                print('--------------------------')
    print('***********************************')


def getWeather(cityname):
    city_name = ""
    for city in citys:
        key = city
        if key in cityname:
            city_name = key

    if city_name=="" and ('天气' in cityname or '多少度' in cityname):
        #print('你输入的城市名有误，或者天气中心未收录你所在城市')
        #return eval("{}")
        city_info=urllib.request.urlopen( 'http://pv.sohu.com/cityjson').read()
        city_info=city_info.decode('gbk')
        newStr = str(city_info).strip('var returnCitySN = ').replace(';','')
        addr=eval(newStr) #取出地址信息
        #open('nj.txt','w').write(addr)
        #pinyin = pinyin.Pinyin(file_path='./nj.txt')
        #pinyin.label_chinese()   #将地址转换成拼音，写入文件

        #f=open('nj.txt').read()
        #provice=f.split('sheng',1)[0].replace(' ','')    #获取省份
        cityname=addr["cname"]
        for city in citys:
            key = city
            if key in cityname:
                city_name = key
    if city_name=="":
        return ""


    url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
    url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
    weather_data = urllib.request.urlopen(url1).read()
    #网址1只需要输入城市名，网址2需要输入城市代码
    #print(url1)
    #读取网页数据
    weather_data = gzip.decompress(weather_data).decode('utf-8')
    #解压网页数据
    weather_dict = json.loads(weather_data)
    
    #将json数据转换为dict数据
    if weather_dict.get('desc') == 'invilad-citykey':
        print('你输入的城市名有误，或者天气中心未收录你所在城市')
        return eval("{}")
    elif weather_dict.get('desc') =='OK':
        forecast = weather_dict.get('data').get('forecast')
        return eval("{'城市':'"+ weather_dict.get('data').get('city')+"','温度':'"+ weather_dict.get('data').get('wendu')+'℃ '+"','建议':'"+ weather_dict.get('data').get('ganmao')\
               +"','风向':'"+ forecast[0].get('fengxiang')+"','风级':'"+ forecast[0].get('fengli')+"','最高气温':'"+ forecast[0].get('high')+"',"  \
               + "'最低气温':'"+ forecast[0].get('low')+"','天气':'"+ forecast[0].get('type')+"','日期':'"+ forecast[0].get('date')+"'}")

#show_weather(get_weather_data())
#print(getWeather('株洲天气怎么样'))
